import {GetMenu} from '@/api/system'


const ServerLocalPathMap = {
  'page/security/user/userList': '/user/list',
  'page/security/role/roleList': '/user/role'
}

function getPrivilegesFromMenu(menu) {

console.log('menu',menu);
  var privileges = {}

  for (var i in menu.children) {

    var module = menu.children[i]

    for (var j in module.children) {

      var subModule = module.children[j]


      var localPath = ServerLocalPathMap[subModule.url]

      if(localPath)
      {
        privileges[localPath] = true

      }

    }

  }



  return privileges
}

const system = {
  state: {
    menu: []
  },

  mutations: {
    SET_MENU: (state, token) => {
      state.token = token
    }
  },

  actions: {

    // 获取系统菜单
    GetMenu({commit, state}) {
      return new Promise((resolve, reject) => {
        GetMenu(state.token).then(data => {
          commit('SET_MENU', getPrivilegesFromMenu(data.obj));
          resolve(data.obj)
        }).catch(error => {
          reject(error)
        })
      })
    }


  }
}

export default system
